<?
namespace Api
{
/** Framework Controller parent
**/
	abstract class	Controller
	{
		protected $m_models=array();	// Model class
		protected $m_view;				// View class
		protected $m_layout;			// Layout class
		protected $m_params;			// URI Parameters
        protected $m_config;			// Module configuration
		
		protected $m_session;			// Sessions variable
        protected $m_autohelper = array(); // Autoloaded helpers  
		
	/////////////////////////////////////////////
		public function __set($_var, $val)
		{
			if(!isset($this->{'m_'.$_var}))
            {
                throw new \Exception('SET: UNKNOWN m_'.$_var.' VAR IN CLASS '.get_class($this));
            }else
			     $this->{'m_'.$_var}=$val;
		}
                
		
		public function __get($_var)
		{
			if(isset($this->{'m_'.$_var}))
            {
                return $this->{'m_'.$_var};
            }
            else
            { 
               if(!empty($this->m_autohelper))
               { 
                    if(array_key_exists($_var, $this->m_autohelper))
                    {         
                        return $this->m_autohelper[$_var];
                    }
                 throw new \Exception('GET: UNKNOWN HELPER '.$_var.' IN CLASS '.get_class($this));
               }else
                 throw new \Exception('GET: UNKNOWN m_'.$_var.' IN CLASS '.get_class($this));   
            }                                          			
		}

        
	/////////////////////////////////////////////
	
		public function __construct(array $config, array $param=array())
		{	
            if(empty($param))
            {
                $param = \Api\Config::$param;
            }
            
			$this->m_params = $param;
			$this->m_config = $config;
			
			$this->m_layout = new \Api\View\Layout($config, $param);	
			if($config['SessionInit'])
			{	
				$this->m_session = new \Api\Session();
				$this->m_layout->_Session=$this->m_session;
				$this->m_layout->_URL = \Api\ObjectLoader::GetBaseURL($param);
			}																			
			$this->initView();
                      	
            $this->initalizeChild();
            		            
		}
        
        /**
         * Call all children methods starting from __init 
         */
        protected function initalizeChild()
        {
            $methods = get_class_methods($this);
            
            foreach($methods as $k => $v)
            {                
                if(preg_match('/__init.*/',$v) == 1)
                {
                    $this->$v();                    
                }
            }
        } 
		
		protected function redirect($url)
		{
			header('Location: http://'.$_SERVER['HTTP_HOST'].$url);
		}
		
		public function initView($viewClass=null){// 
			// in config[LayoutPtr] pass layout smarty			
	
			$this->m_config['LayoutPtr']=&$this->m_layout->GetSmarty();			
			$this->m_view = new View\Content($this->m_config, $this->m_params);
			$this->m_view->CONTROLER = $this;
            $this->m_view->__model = $this->m_params[\Api\ObjectLoader::C_MODULE];
            
            if(array_key_exists('LayoutTitle',$this->m_config))
            {
                $this->m_layout->SetTitle($this->m_config['LayoutTitle']);
            }
           
		}
		
		public function __destruct(){			
			if($this->m_layout){ $this->m_layout->display(); }
			unset($this->m_view);
			unset($this->m_layout);
		}
		
        /**
         * Assign helper 
         */
        public function SetAutoHelper(&$helper, $alias)
        {            
            $this->m_autohelper[$alias] = $helper;
            return $helper;
        }
        
		abstract public function indexAction(array $params=array());
	}
}
?>